package com.xinjian.jianba.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.xinjian.jianba.dao.dwmid.UserInfoDao;
import com.xinjian.jianba.dao.dwmid.UserMediaDao;
import com.xinjian.jianba.dto.PerEditerDto;
import com.xinjian.jianba.dto.PersonEditerDto;
import com.xinjian.jianba.enity.UserInfoEnity;
import com.xinjian.jianba.enity.UserMediaEnity;
import com.xinjian.jianba.exception.BusinessException;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.QiNiuService;
import com.xinjian.jianba.utils.JsonUtil;
import com.xinjian.jianba.utils.QiNiuUtil;
import com.xinjian.jianba.utils.RongYunUtil;
@Service
public class QiNiuServiceImpl implements QiNiuService{

	@Autowired
	private UserMediaDao userMediaDao;
	@Autowired
	private UserInfoDao userInfoDao;
	@Override
	public String savePersonalDataeEitor(String jsonStr,String address) {
		
		PersonEditerDto personEditerDto=JsonUtil.toBean(jsonStr, PersonEditerDto.class);
		Date date = new Date();
		String userId=personEditerDto.getUserId();
		if("null".equals(userId)||userId==null||userId.equals("")){
			return "用户id不能为空";
		}
		Timestamp timestamp = new Timestamp(date.getTime());
		UserMediaEnity userMediaEnity=new UserMediaEnity();
		userMediaEnity.setAddress(address);
	    userMediaEnity.setBucket(personEditerDto.getBucket());
		userMediaEnity.setCreTime(timestamp);
		userMediaEnity.setFileName(personEditerDto.getFilename());
		userMediaEnity.setHash(personEditerDto.getHash());
		userMediaEnity.setQnkey(personEditerDto.getKey());
		userMediaEnity.setMtype(personEditerDto.getAction());
		userMediaEnity.setTimeAdd(date);
		userMediaEnity.setType(0);
		userMediaEnity.setUid(Long.parseLong(userId));
	    try{
		userMediaDao.insert(userMediaEnity);
		}catch(DuplicateKeyException e){
			return "文件名key重复,地址为:"+address;
		}
		return "SUCCESS";
	}
	@Override
	public Map<String,Object> upLogo(String userId, String address) {
		UserInfoEnity userInfoEnity=userInfoDao.findById(Long.parseLong(userId));
		String dbLogo=userInfoEnity.getUserLogo();
		int lo=dbLogo.lastIndexOf("=");
		Integer index=null;
		if(lo>0){
		index=Integer.parseInt(dbLogo.substring(lo+1).trim())+1;
		}else{
			index=1;
		}
		address=address+"?m="+index;
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("address", address);
		map.put("userId", userId);
		int re=userInfoDao.updateQiNiuLogo(map);
		map.put("re", re);
		return map;
	}
	@Override
	public String deletePerEditor(PerEditerDto PerEditerDto) {
		Long userId=PerEditerDto.getUserId();
		String[] address=PerEditerDto.getUrl();
		if(address.length==0){
			throw new BusinessException(ResponseCode.BAD_REQUEST, "url地址内容不能为空");
		}
		StringBuffer reStr=new StringBuffer();
		for(String keyAddress:address){
			reStr.append(keyAddress.substring(keyAddress.indexOf(QiNiuUtil.domainOfBucket)+1+QiNiuUtil.domainOfBucket.length())+",") ;
		}
		String[] keyList=reStr.toString().split(",");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("addressList", address);
		int result=userMediaDao.deleteByUidAndAddress(map);
		if(result==address.length){
		QiNiuUtil.delete(keyList);
		 return "SUCCESS";
		}else{
		 throw new BusinessException(ResponseCode.ERROR, "目标资源不存在");
		}
		
	}
	@Override
	public String updateRongYunToken(String userId, String address) throws Exception {
		String nickName=userInfoDao.selectNicknameByuserId(Long.parseLong(userId));
		//更新融云用户信息
		RongYunUtil.updateRongYunToken(userId, nickName, address);
		return null;
	}
}
